package com.example.hjt_java_web_homework3.service;

import com.example.hjt_java_web_homework3.dao.DeviceDao;
import com.example.hjt_java_web_homework3.model.Device;

import java.util.List;

/**
 * @author Li Chun Hei
 * @date 31/10/2025
 * @description
 */
public class DeviceService {
    private DeviceDao deviceDao = new DeviceDao();

    public List<Device> getAllDevices() {
        return deviceDao.findAll();
    }

    public Device getDeviceById(Long id) {
        return deviceDao.findById(id);
    }

    public boolean createDevice(Device device) {
        // 检查设备ID是否已存在
        if (deviceDao.findByDeviceId(device.getDeviceId()) != null) {
            return false;
        }
        return deviceDao.save(device);
    }

    public boolean updateDevice(Device device) {
        Device existingDevice = deviceDao.findById(device.getId());
        if (existingDevice == null) {
            return false;
        }
        return deviceDao.update(device);
    }

    public boolean deleteDevice(Long id) {
        return deviceDao.delete(id);
    }

    public List<Device> getDevicesByType(String type) {
        List<Device> allDevices = deviceDao.findAll();
        return allDevices.stream()
                .filter(device -> device.getType().equals(type))
                .collect(java.util.stream.Collectors.toList());
    }}
